package com.applicationLayer.action;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
 
public class FileUploadAction extends ActionSupport implements  ServletRequestAware {
   
	private static final long serialVersionUID = 1L;
	private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private String destPath;
    private HttpServletRequest servletRequest;
 
    public String execute() {
        try {
        	destPath = "C:/Users/Laurent/Desktop/training/CarSharingApplication/upload";
            String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
            System.out.println("Server path:" + filePath);
            System.out.println("Desth path:" + destPath);
            File fileToCreate = new File(destPath, this.userImageFileName);
 
            FileUtils.copyFile(this.userImage, fileToCreate);
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.getMessage());
 
            return INPUT;
        }
        return SUCCESS;
    }
 
    public File getUserImage() {
        return userImage;
    }
 
    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }
 
    public String getUserImageContentType() {
        return userImageContentType;
    }
 
    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }
 
    public String getUserImageFileName() {
        return userImageFileName;
    }
 
    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }
 
    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
 
    }

	/**
	 * @return the destPath
	 */
	public String getDestPath() {
		return destPath;
	}

	/**
	 * @param destPath the destPath to set
	 */
	public void setDestPath(String destPath) {
		this.destPath = destPath;
	}
}